package com.zggk.lzz.lzmlbj;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.blankj.utilcode.util.ToastUtils;
import com.zggk.lzz.R;
import com.zggk.lzz.adapter.LGRecycleViewAdapter;
import com.zggk.lzz.adapter.LGViewHolder;
import com.zggk.lzz.lzml.Lzml2Bean;
import com.zggk.lzz.lzml.LzmlBean;
import com.zggk.lzz.lzml.SeasonpListener;
import com.zggk.lzz.lzml.SeasonpPopwindow;
import com.zggk.lzz.lzmladd.LzmlAddActivity;
import com.zggk.lzz.lzmlbj2.LzmlBj2Activity;
import com.zggk.lzz.mvp.MVPBaseActivity;
import com.zggk.lzz.util.NoRecycleView;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class LzmlBjActivity extends MVPBaseActivity<LzmlBjContract.View, LzmlBjPresenter> implements LzmlBjContract.View {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.cancle_button)
    TextView cancleButton;
    @Bind(R.id.view_lay)
    LinearLayout viewLay;
    @Bind(R.id.sj_et)
    EditText sjEt;
    @Bind(R.id.xj_et)
    EditText xjEt;
    @Bind(R.id.xjj_et)
    EditText xjjEt;
    @Bind(R.id.cj_et)
    EditText cjEt;
    @Bind(R.id.cx_te)
    TextView cxTe;
    @Bind(R.id.te_lay)
    LinearLayout teLay;
    @Bind(R.id.views)
    View views;
    @Bind(R.id.x_num_te)
    TextView xNumTe;
    @Bind(R.id.xj_re)
    NoRecycleView xjRe;
    @Bind(R.id.xj_num_te)
    TextView xjNumTe;
    @Bind(R.id.xiang_re)
    NoRecycleView xiangRe;
    @Bind(R.id.c_num_te)
    TextView cNumTe;
    @Bind(R.id.cun_re)
    NoRecycleView cunRe;
    @Bind(R.id.disease_new_parent_layout)
    RelativeLayout diseaseNewParentLayout;
    private LGRecycleViewAdapter<LzmlBean.DataBean.XianlzBean> xianadapter;
    private LGRecycleViewAdapter<LzmlBean.DataBean.XianglzBean> xiangadapter;
    private LGRecycleViewAdapter<LzmlBean.DataBean.CunlzBean> cunadapter;

    private List<LzmlBean.DataBean.XianlzBean> XianListDatas = new ArrayList();
    private List<LzmlBean.DataBean.XianglzBean> XiangListDatas = new ArrayList();
    private List<LzmlBean.DataBean.CunlzBean> CunListDatas = new ArrayList();

    private Boolean TYPE = true;
    private String SJE = "";
    private String XJE = "";
    private String XJJE = "";
    private String CJE = "";
    private ArrayList<String> listData = new ArrayList<>();
    private SeasonpPopwindow sjPop;
    @Override
    public void onRequestError(String msg) {
        XianListDatas.clear();
        xianadapter.notifyDataSetChanged();
        ToastUtils.showShort(msg);
    }

    @Override
    protected int getLayout() {
        return R.layout.act_lzml_bj;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        initXianAdapter();
        initXiangAdapter();
        initCunAdapter();
        listener();
        initpopw();
        initData();
        initView();
    }

    private void initView() {
        setTitleText("路长名录");
        goBack();
        cancleButton.setVisibility(View.VISIBLE);
        cancleButton.setText("完成编辑");
    }

    private void initData() {
        mPresenter.getData(sjEt.getText().toString(), xjEt.getText().toString(), xjjEt.getText().toString(), cjEt.getText().toString(), LzmlBjActivity.this);
    }

    private void initpopw() {
        sjPop = new SeasonpPopwindow(LzmlBjActivity.this, listData, new SeasonpListener() {
            @Override
            public void selectPosition(int position) {
                TYPE = false;
                if (listData.get(position).split(" ").length == 4) {
                    sjEt.setText(listData.get(position).split(" ")[0]);
                    SJE = sjEt.getText().toString();
                    xjEt.setText(listData.get(position).split(" ")[1]);
                    XJE = xjEt.getText().toString();
                    xjjEt.setText(listData.get(position).split(" ")[2]);
                    XJJE = xjjEt.getText().toString();
                    cjEt.setText(listData.get(position).split(" ")[3]);
                    CJE = cjEt.getText().toString();
                } else if (listData.get(position).split(" ").length == 3) {
                    sjEt.setText(listData.get(position).split(" ")[0]);
                    SJE = sjEt.getText().toString();
                    xjEt.setText(listData.get(position).split(" ")[1]);
                    XJE = xjEt.getText().toString();
                    xjjEt.setText(listData.get(position).split(" ")[2]);
                    XJJE = xjjEt.getText().toString();
                } else if (listData.get(position).split(" ").length == 2) {
                    sjEt.setText(listData.get(position).split(" ")[0]);
                    SJE = sjEt.getText().toString();
                    xjEt.setText(listData.get(position).split(" ")[1]);
                    XJE = xjEt.getText().toString();
                } else if (listData.get(position).split(" ").length == 1) {
                    sjEt.setText(listData.get(position).split(" ")[0]);
                    SJE = sjEt.getText().toString();
                }
                TYPE = true;
                sjPop.close();
            }
        });
    }

    @Override
    public void getDatas(LzmlBean.DataBean dataBean) {
        XianListDatas.clear();
        XianListDatas.addAll(dataBean.getXianlz());
        xianadapter.notifyDataSetChanged();
        xNumTe.setText("共" + dataBean.getXianlzCount() + "人");
        XiangListDatas.clear();
        XiangListDatas.addAll(dataBean.getXianglz());
        xiangadapter.notifyDataSetChanged();
        xjNumTe.setText("共" + dataBean.getXianglzCount() + "人");
        CunListDatas.clear();
        CunListDatas.addAll(dataBean.getCunlz());
        cunadapter.notifyDataSetChanged();
        cNumTe.setText("共" + dataBean.getCunlzCount() + "人");
    }

    @Override
    public void getSDatas(List<Lzml2Bean.DataBean> dataBean) {
        listData.clear();
        for (int i = 0; i < dataBean.size(); i++) {
            if (dataBean.get(i).getCun() != null) {
                listData.add(dataBean.get(i).getShi() + " " +
                        dataBean.get(i).getXian() + " " +
                        dataBean.get(i).getXiang() + " " +
                        dataBean.get(i).getCun());
            } else if (dataBean.get(i).getXiang() != null) {
                listData.add(dataBean.get(i).getShi() + " " +
                        dataBean.get(i).getXian() + " " +
                        dataBean.get(i).getXiang());
            } else if (dataBean.get(i).getXian() != null) {
                listData.add(dataBean.get(i).getShi() + " " +
                        dataBean.get(i).getXian());
            } else if (dataBean.get(i).getShi() != null) {
                listData.add(dataBean.get(i).getShi());
            }
        }
        sjPop.notifityData();
        sjPop.show(views);
    }

    @Override
    public void deletes() {

    }

    private void initCunAdapter() {
        cunRe.setHasFixedSize(true);
        cunRe.setNestedScrollingEnabled(false);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        cunRe.setLayoutManager(manager);
        cunadapter = new LGRecycleViewAdapter<LzmlBean.DataBean.CunlzBean>(CunListDatas) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_lzml_bj;
            }

            @Override
            public void convert(LGViewHolder holder, final LzmlBean.DataBean.CunlzBean zCDBean, final int position) {
                if (zCDBean.getOnlineFlag().equals("1")){
                    holder.setText(R.id.state, "(在线)");
                    holder.setTextColor(R.id.state, getResources().getColor(R.color.two_color));
                }else {
                    holder.setText(R.id.state, "(离线)");
                    holder.setTextColor(R.id.state, getResources().getColor(R.color.red_color));
                }
                holder.setText(R.id.name_te, zCDBean.getAdministratorNameName());
                if (zCDBean.getRoadAdministratorLevel().equals("总路长")) {
                    holder.setColor(R.id.mc_te, getResources().getColor(R.color.four_color));
                } else if (zCDBean.getRoadAdministratorLevel().equals("县路长")){
                    holder.setColor(R.id.mc_te, getResources().getColor(R.color.one_color));
                }else {
                    holder.setColor(R.id.mc_te, getResources().getColor(R.color.two_color));
                }
                holder.setText(R.id.mc_te, zCDBean.getRoadAdministratorLevel());
                holder.setText(R.id.dz_te, zCDBean.getOrgName());
                TextView de_te = (TextView)holder.getView(R.id.de_te);
                de_te.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPresenter.delete(CunListDatas.get(position).getId(),LzmlBjActivity.this);
                    }
                });
                TextView ddh_te = (TextView)holder.getView(R.id.ddh_te);
                ddh_te.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent= new Intent(LzmlBjActivity.this,LzmlBj2Activity.class);
                        intent.putExtra("Id",CunListDatas.get(position).getId());
                        startActivity(intent);
                    }
                });
            }
        };
        cunRe.setAdapter(cunadapter);
    }

    private void initXiangAdapter() {
        xiangRe.setHasFixedSize(true);
        xiangRe.setNestedScrollingEnabled(false);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        xiangRe.setLayoutManager(manager);
        xiangadapter = new LGRecycleViewAdapter<LzmlBean.DataBean.XianglzBean>(XiangListDatas) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_lzml_bj;
            }

            @Override
            public void convert(LGViewHolder holder, final LzmlBean.DataBean.XianglzBean zCDBean, final int position) {
                if (zCDBean.getOnlineFlag().equals("1")){
                    holder.setText(R.id.state, "(在线)");
                    holder.setTextColor(R.id.state, getResources().getColor(R.color.two_color));
                }else {
                    holder.setText(R.id.state, "(离线)");
                    holder.setTextColor(R.id.state, getResources().getColor(R.color.red_color));
                }
                holder.setText(R.id.name_te, zCDBean.getAdministratorNameName());
                if (zCDBean.getRoadAdministratorLevel().equals("总路长")) {
                    holder.setColor(R.id.mc_te, getResources().getColor(R.color.four_color));
                } else if (zCDBean.getRoadAdministratorLevel().equals("县路长")){
                    holder.setColor(R.id.mc_te, getResources().getColor(R.color.one_color));
                }else {
                    holder.setColor(R.id.mc_te, getResources().getColor(R.color.two_color));
                }
                holder.setText(R.id.mc_te, zCDBean.getRoadAdministratorLevel());
                holder.setText(R.id.dz_te, zCDBean.getOrgName());
                TextView de_te = (TextView)holder.getView(R.id.de_te);
                de_te.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPresenter.delete(XiangListDatas.get(position).getId(),LzmlBjActivity.this);
                    }
                });
                TextView ddh_te = (TextView)holder.getView(R.id.ddh_te);
                ddh_te.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent= new Intent(LzmlBjActivity.this,LzmlBj2Activity.class);
                        intent.putExtra("Id",XiangListDatas.get(position).getId());
                        startActivity(intent);
                    }
                });
            }
        };
        xiangRe.setAdapter(xiangadapter);
    }

    private void initXianAdapter() {
        xjRe.setHasFixedSize(true);
        xjRe.setNestedScrollingEnabled(false);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        xjRe.setLayoutManager(manager);
        xianadapter = new LGRecycleViewAdapter<LzmlBean.DataBean.XianlzBean>(XianListDatas) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_lzml_bj;
            }

            @Override
            public void convert(LGViewHolder holder, final LzmlBean.DataBean.XianlzBean zCDBean, final int position) {
                if (zCDBean.getOnlineFlag().equals("1")){
                    holder.setText(R.id.state, "(在线)");
                    holder.setTextColor(R.id.state, getResources().getColor(R.color.two_color));
                }else {
                    holder.setText(R.id.state, "(离线)");
                    holder.setTextColor(R.id.state, getResources().getColor(R.color.red_color));
                }
                holder.setText(R.id.name_te, zCDBean.getAdministratorNameName());
                if (zCDBean.getRoadAdministratorLevel().equals("总路长")) {
                    holder.setColor(R.id.mc_te, getResources().getColor(R.color.four_color));
                } else if (zCDBean.getRoadAdministratorLevel().equals("县路长")){
                    holder.setColor(R.id.mc_te, getResources().getColor(R.color.one_color));
                }else {
                    holder.setColor(R.id.mc_te, getResources().getColor(R.color.two_color));
                }
                holder.setText(R.id.mc_te, zCDBean.getRoadAdministratorLevel());
                holder.setText(R.id.dz_te, zCDBean.getOrgName());
                TextView de_te = (TextView)holder.getView(R.id.de_te);
                de_te.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPresenter.delete(XianListDatas.get(position).getId(),LzmlBjActivity.this);
                    }
                });
                TextView ddh_te = (TextView)holder.getView(R.id.ddh_te);
                ddh_te.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent= new Intent(LzmlBjActivity.this,LzmlBj2Activity.class);
                        intent.putExtra("Id",XianListDatas.get(position).getId());
                        startActivity(intent);
                    }
                });
            }
        };
        xjRe.setAdapter(xianadapter);
    }

    private void listener() {
        sjEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                //如果长度为0
                if (TYPE == true) {
                    mPresenter.getSData(sjEt.getText().toString(), "", "", "");
                }
            }
        });

        xjEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                //如果长度为0
                if (TYPE == true) {
                    mPresenter.getSData("", xjEt.getText().toString(), "", "");
                }
            }
        });

        xjjEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                //如果长度为0
                if (TYPE == true) {
                    mPresenter.getSData("", "", xjjEt.getText().toString(), "");
                }
            }
        });

        cjEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                //如果长度为0
                if (TYPE == true) {
                    mPresenter.getSData("", "", "", cjEt.getText().toString());
                }
            }
        });
    }

    @OnClick({R.id.cx_te,R.id.cancle_button,R.id.save_te})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cx_te:
                mPresenter.getData(SJE, XJE, XJJE, CJE, LzmlBjActivity.this);
                break;
            case R.id.cancle_button:
               finish();
                break;
            case R.id.save_te:
                Intent intent= new Intent(LzmlBjActivity.this,LzmlAddActivity.class);
                startActivity(intent);
                break;
        }
    }
}
